﻿using System.Windows.Controls;
using System.Windows;
using Notoric.Commanding;

namespace Notoric.View.Music.CWNotation.Controls
{
    /// <summary>
    /// Visualizes a dynamic mark.
    /// </summary>
    public partial class DynamicMark : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicMark"/> class.
        /// </summary>
        public DynamicMark()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Click event of the menu_MarkType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menu_MarkType_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem)sender;
            byte degree = 1;
            ViewModel.Elements.CWNotation.DynamicMarkType markType;

            switch (menuItem.Name)
            {
                case "menu_PianoPianissimo":
                    degree = 3;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.Piano;
                    break;
                case "menu_Pianissimo":
                    degree = 2;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.Piano;
                    break;
                case "menu_Piano":
                    degree = 1;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.Piano;
                    break;
                case "menu_MezzoPiano":
                    degree = 1;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.MezzoPiano;
                    break;
                case "menu_MezzoForte":
                    degree = 1;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.MezzoForte;
                    break;
                case "menu_Forte":
                    degree = 1;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.Forte;
                    break;
                case "menu_Fortissimo":
                    degree = 2;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.Forte;
                    break;
                case "menu_ForteFortissimo":
                    degree = 3;
                    markType = ViewModel.Elements.CWNotation.DynamicMarkType.Forte;
                    break;
                default:
                    return;
            }

            var mark = DataContext as ViewModel.Elements.CWNotation.DynamicMark;
            if (mark != null)
            {
                var cmdDegree = mark.CreateSetDegreeCommand(degree);
                var cmdType = mark.CreateSetMarkTypeCommand(markType);
                var macro = new UndoableMacroCommand(mark.UndoStack, mark.RedoStack)
                {
                    Name = cmdType.Name
                };

                macro.Commands.Add(cmdType);
                macro.Commands.Add(cmdDegree);

                macro.Execute(null);
            }
        }

        /// <summary>
        /// Handles the Checked event of the menu_IsSubito control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menu_IsSubito_Checked(object sender, RoutedEventArgs e)
        {
            var mark = DataContext as ViewModel.Elements.CWNotation.DynamicMark;
            if (mark != null)
            {
                var cmd = mark.CreateSetIsSubitoCommand(true);
                cmd.Execute(null);
            }
        }

        /// <summary>
        /// Handles the Unchecked event of the menu_IsSubito control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menu_IsSubito_Unchecked(object sender, RoutedEventArgs e)
        {
            var mark = DataContext as ViewModel.Elements.CWNotation.DynamicMark;
            if (mark != null)
            {
                var cmd = mark.CreateSetIsSubitoCommand(false);
                cmd.Execute(null);
            }
        }

        /// <summary>
        /// Handles the Click event of the menu_Expression control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menu_Expression_Click(object sender, RoutedEventArgs e)
        {
            var mark = DataContext as ViewModel.Elements.CWNotation.DynamicMark;
            if (mark != null)
            {
                var cmd = mark.CreateSetExpressionCommand(((MenuItem)sender).Tag.ToString());
                cmd.Execute(null);
            }
        }

        /// <summary>
        /// Handles the Click event of the menu_ClearExpression control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void menu_ClearExpression_Click(object sender, RoutedEventArgs e)
        {
            var mark = DataContext as ViewModel.Elements.CWNotation.DynamicMark;
            if (mark != null)
            {
                var cmd = mark.CreateSetExpressionCommand("");
                cmd.Execute(null);
            }
        }

        #endregion
    }
}
